package com.qiujie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qiujie.dto.Response;
import com.qiujie.dto.ResponseDTO;
import com.qiujie.entity.DishCategory;
import com.qiujie.mapper.DishCategoryMapper;
import com.qiujie.service.DishCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 饮食分类控制器
 */
@RestController
@RequestMapping("/dish/category")
@Api(tags = "饮食分类管理")
public class DishCategoryController {

    @Autowired
    private DishCategoryService dishCategoryService;

    @Autowired
    private DishCategoryMapper dishCategoryMapper;

    @PostMapping("/add")
    @ApiOperation("添加分类")
    @PreAuthorize("hasAnyAuthority('dishtype:add')")
    public ResponseDTO add(@RequestBody DishCategory dishCategory) {
        return this.dishCategoryService.add(dishCategory);
    }

    @PutMapping("/update")
    @ApiOperation("更新饮食分类")
    @PreAuthorize("hasAnyAuthority('dishtype:update')")
    public ResponseDTO update(@RequestBody DishCategory dishCategory) {
        return this.dishCategoryService.update(dishCategory);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除饮食分类")
    @PreAuthorize("hasAnyAuthority('dishtype:delete')")
    public ResponseDTO delete(@PathVariable Integer id) {
        return this.dishCategoryService.delete(id);
    }

    @GetMapping("/list")
    @ApiOperation("获取饮食分类列表")
    public ResponseDTO list() {
            ResponseDTO responseDTO = new ResponseDTO();
            LambdaQueryWrapper<DishCategory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByAsc(DishCategory::getSort);
            List<DishCategory> list = dishCategoryMapper.selectList(queryWrapper);
            responseDTO.setData(list);
            return Response.success(responseDTO);
    }
} 